package com.eshop.modules.user.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.api.EshopException;
import com.eshop.common.service.impl.BaseServiceImpl;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.dozer.service.IGenerator;
import com.eshop.enums.BillDetailEnum;
import com.eshop.enums.OrderInfoEnum;
import com.eshop.enums.PayTypeEnum;
import com.eshop.event.TemplateBean;
import com.eshop.event.TemplateEvent;
import com.eshop.event.TemplateListenEnum;
import com.eshop.modules.user.domain.ShopUser;
import com.eshop.modules.user.domain.UserRecharge;
import com.eshop.modules.user.service.UserBillService;
import com.eshop.modules.user.service.UserRechargeService;
import com.eshop.modules.user.service.dto.UserRechargeDto;
import com.eshop.modules.user.service.dto.UserRechargeQueryCriteria;
import com.eshop.modules.user.service.mapper.UserMapper;
import com.eshop.modules.user.service.mapper.UserRechargeMapper;
import com.eshop.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = {Exception.class})
@Service
public class UserRechargeServiceImpl extends BaseServiceImpl<UserRechargeMapper, UserRecharge> implements UserRechargeService {
    @Autowired
    private UserBillService billService;
    @Autowired
    private IGenerator generator;
    @Autowired
    private ApplicationEventPublisher publisher;
    @Autowired
    private UserMapper yxUserMapper;
    @Autowired
    private UserRechargeMapper yxUserRechargeMapper;

    @Override // com.eshop.modules.user.service.UserRechargeService
    public void updateRecharge(UserRecharge userRecharge) {
        ShopUser user =  this.yxUserMapper.selectById(userRecharge.getUid());
        userRecharge.setPaid(OrderInfoEnum.PAY_STATUS_1.getValue());
        userRecharge.setPayTime(new Date());
        this.yxUserRechargeMapper.updateById(userRecharge);
        BigDecimal newPrice = NumberUtil.add(userRecharge.getPrice(), user.getNowMoney()).add(userRecharge.getGivePrice());
        this.billService.income(userRecharge.getUid(), "用户余额充值", BillDetailEnum.CATEGORY_1.getValue(), BillDetailEnum.TYPE_1.getValue(), userRecharge.getPrice().doubleValue(), newPrice.doubleValue(), "成功充值余额" + userRecharge.getPrice(), userRecharge.getId().toString());
        user.setNowMoney(newPrice);
        this.yxUserMapper.updateById(user);
        this.publisher.publishEvent(new TemplateEvent(this, TemplateBean.builder().time(DateUtil.formatTime(userRecharge.getPayTime())).price(userRecharge.getPrice().toString()).orderId(userRecharge.getOrderId()).uid(userRecharge.getUid()).templateType(TemplateListenEnum.TYPE_4.getValue()).build()));
    }

    @Override // com.eshop.modules.user.service.UserRechargeService
    public UserRecharge getInfoByOrderId(String orderId) {
        UserRecharge userRecharge = new UserRecharge();
        userRecharge.setOrderId(orderId);
        return (UserRecharge) this.yxUserRechargeMapper.selectOne(Wrappers.query(userRecharge));
    }

    @Override // com.eshop.modules.user.service.UserRechargeService
    public String addRecharge(ShopUser user, String price, String paidPrice) {
        if (StrUtil.isBlank(price) || StrUtil.isBlank(paidPrice)) {
            throw new EshopException("参数非法");
        }
        UserRecharge userRecharge = new UserRecharge();
        String orderSn = IdUtil.getSnowflake(0, 0).nextIdStr();
        userRecharge.setNickname(user.getNickname());
        userRecharge.setOrderId(orderSn);
        userRecharge.setUid(user.getUid());
        userRecharge.setPrice(new BigDecimal(price));
        userRecharge.setGivePrice(new BigDecimal(paidPrice));
        userRecharge.setRechargeType(PayTypeEnum.WEIXIN.getValue());
        userRecharge.setPaid(OrderInfoEnum.PAY_STATUS_0.getValue());
        this.yxUserRechargeMapper.insert(userRecharge);
        return orderSn;
    }

    @Override // com.eshop.modules.user.service.UserRechargeService
    public Map<String, Object> queryAll(UserRechargeQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<UserRecharge> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", this.generator.convert(page.getList(), UserRechargeDto.class));
        map.put("totalElements", Long.valueOf(page.getTotal()));
        return map;
    }

    @Override // com.eshop.modules.user.service.UserRechargeService
    public List<UserRecharge> queryAll(UserRechargeQueryCriteria criteria) {
        return this.baseMapper.selectList(QueryHelpPlus.getPredicate(UserRecharge.class, criteria));
    }

    @Override // com.eshop.modules.user.service.UserRechargeService
    public void download(List<UserRechargeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserRechargeDto yxUserRecharge : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("充值用户UID", yxUserRecharge.getUid());
            map.put("订单号", yxUserRecharge.getOrderId());
            map.put("充值金额", yxUserRecharge.getPrice());
            map.put("充值类型", yxUserRecharge.getRechargeType());
            map.put("是否充值", yxUserRecharge.getPaid());
            map.put("充值支付时间", yxUserRecharge.getPayTime());
            map.put("退款金额", yxUserRecharge.getRefundPrice());
            map.put("昵称", yxUserRecharge.getNickname());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
